#获取参数
typeset MIP='@@MIP@@'               #主服务器ip
typeset dbmuser='@@dbmuser@@'                  #主服务器mysql用户
typeset dbmpassword='@@dbmpassword@@'     #主服务器mysql密码
typeset check_times='@@check_times@@'                  #命令重复次数
typeset mconfpath='@@mconfpath@@'         #主mysql配置文件路径

typeset MIP='10.27.3.175'               #主服务器ip
typeset dbmuser='root'                  #主服务器mysql用户
typeset dbmpassword='QWEasdzxc#123'     #主服务器mysql密码
typeset check_times=''                  #命令重复次数
typeset mconfpath='/etc/my.cnf'         #主mysql配置文件路径
#检查check次数
[ ! ${check_times} ] && typeset check_times='3'
expr 1 + ${check_times} &>/dev/null
if [ $? -ne 0 ];then
    log_format "ERROR" "script_execute_error!" && exit 1
fi

#配置日志路径
typeset SCRIPT_NAME="mysql_master_$MIP"
typeset LOG_PATH='/tmp/dr_log'
[ ! -d "${LOG_PATH}" ] && mkdir -p ${LOG_PATH}
typeset LOG_FILE="${LOG_PATH}/${SCRIPT_NAME}.log"
[ ! -f "${LOG_FILE}" ] && touch ${LOG_FILE}

function log_format()
{
    echo "[`date +"%F %H:%M:%S"`] [$*]" | tee -a ${LOG_FILE}
}

# log_format "begin to build mysql master................................."


#检查配置文件是否存在
if [ ! "$mconfpath" ];then
    cnfnum=`find / -name my.cnf|wc -l`
    if [ "$cnfnum" -gt 1 ];then
         log_format "ERROR" "script_execute_error!" && exit 1
    else
        mconfpath=`find / -name my.cnf `
        # log_format 'INFO' "find my.cnf path: $mconfpath"
    fi
else
    if [ ! -f "$mconfpath" ];then
        log_format "ERROR" "script_execute_error!" && exit 1
    # else
    #     log_format 'INFO' "my.cnf exits"
    fi
fi



#备份配置文件
time=`date +%Y%m%d`
backpath=${mconfpath}_${time}.bak
if [ ! -f "$backpath" ];then
   cp $mconfpath $backpath
    if [ ! -f "$backpath" ];then
         log_format "ERROR" "script_execute_error!" && exit 1
    # else
    #     log_format 'INFO' "copy my.cnf success"
    fi
# else
#     log_format 'INFO' "my.cnf copy exits"
fi

#检查mysql命令路径
basedir=`cat $mconfpath|grep -v ^#|grep basedir|awk -F'=' '{print $2}'| sed -e 's/^[ ]*//g' | sed -e 's/[ ]*$//g'`
if [ ! "$basedir" ];then
     log_format "ERROR" "script_execute_error!" && exit 2
else  
    mycmdpath=$basedir/bin/mysql
    if [ ! -e "$mycmdpath" ];then
         log_format "ERROR" "script_execute_error!" && exit 2
    # else
    #     log_format 'INFO' "mysql cmd finded $mycmdpath"
    fi
fi

cat $mconfpath|grep -v ^#|grep server-id > /dev/null
jd1=`echo $?`
cat $mconfpath|grep -v ^#|grep log-bin > /dev/null
jd2=`echo $?`

if [ $jd1 -ne 0 ] && [ $jd2 -ne 0 ];then
    #检查主配置文件配置
    ##检查或修改slave-skip-errors=all
    cat $mconfpath|grep -v ^#|grep slave-skip-errors > /dev/null
    if [ $? -ne 0 ];then
        sed  -i '/\[mysqld\]/a slave-skip-errors=all' $mconfpath
        # log_format 'INFO' "slave-skip-errors added"
    else
        sed -i 's/slave-skip-erros.*/slave-skip-errors=all/' $mconfpath
        # log_format 'INFO' "slave-skip-error changed"
    fi

    ##检查或修改log-bin=mysql-bin-master
    cat $mconfpath|grep -v ^#|grep log-bin > /dev/null
    if [ $? -ne 0 ];then
        sed  -i '/\[mysqld\]/a log-bin=mysql-bin-master' $mconfpath
        # log_format 'INFO' "log-bin added"
    else
        sed -i 's/log-bin.*/log-bin=mysql-bin-master/' $mconfpath
        # log_format 'INFO' "log-bin changed"
    fi

    ##添加或修改server-id
    ID=`echo $MIP|awk -F'.' '{print 1$3$4}'| sed -e 's/^[ ]*//g' | sed -e 's/[ ]*$//g'`
    cat $mconfpath|grep -v ^#|grep server-id > /dev/null
    if [ $? -ne 0 ];then
        sed  -i "/\[mysqld\]/a server-id=$ID" $mconfpath
        # log_format 'INFO' "server-id added"
    else
        sed -i "s/server-id.*/server-id=$ID/" $mconfpath
        # log_format 'INFO' "server-id changed"
    fi

    ##删除gtid_mode配置
    gtidnum1=`cat $mconfpath|grep -v ^#|grep 'gtid_mode'|wc -l`
    gtidnum2=`cat $mconfpath|grep -v ^#|grep 'gtid-mode'|wc -l`
    if [ $gtidnum1 -eq 0 ] && [ $gtidnum2 -eq 0 ];then
        # log_format 'INFO' 'no gtid information in my.cnf'
        echo 'no gtid information in my.cnf' >> ${LOG_FILE}
    else
        sed -i "/gtid_mode/d" $mconfpath
        sed -i "/gtid-mode/d" $mconfpath
        gtidnum1=`cat $mconfpath|grep -v ^#|grep 'gtid_mode'|wc -l`
        gtidnum2=`cat $mconfpath|grep -v ^#|grep 'gtid-mode'|wc -l`
        if [ $gtidnum1 -eq 0 ] && [ $gtidnum2 -eq 0 ];then
            # log_format 'INFO' 'gtid information has been deleted in my.cnf'
            echo 'gtid information has been deleted in my.cnf' >> ${LOG_FILE}
        else
            log_format 'ERROR' 'failed to delete gtid information in my.cnf' && log_format "ERROR" "script_execute_error!" && exit 1
        fi
    fi

    ##删除enforce_gtid_consistenc配置
    enforcenum1=`cat $mconfpath|grep -v ^#|grep 'enforce_gtid_consistency'|wc -l`
    enforcenum2=`cat $mconfpath|grep -v ^#|grep 'enforce-gtid-consistency'|wc -l`
    if [ $enforcenum1 -eq 0 ] && [ $enforcenum2 -eq 0 ];then
        # log_format 'INFO' 'no enforce information in my.cnf'
        echo  'no enforce information in my.cnf' >> ${LOG_FILE}
    else
        sed -i "/enforce_gtid_consistency/d" $mconfpath
        sed -i "/enforce-gtid-consistency/d" $mconfpath
        enforcenum1=`cat $mconfpath|grep -v ^#|grep 'enforce_gtid_consistency'|wc -l`
        enforcenum2=`cat $mconfpath|grep -v ^#|grep 'enforce-gtid-consistency'|wc -l`
        if [ $enforcenum1 -eq 0 ] && [ $enforcenum2 -eq 0 ];then
            # log_format 'INFO' 'enforce information has been deleted in my.cnf'
            echo 'enforce information has been deleted in my.cnf' >> ${LOG_FILE}
        else
            log_format 'ERROR' 'failed to delete enforce information in my.cnf' && exit 1
        fi
    fi

    #重启mysql
    # log_format 'INFO' "begin to restart mysql"
    systemctl restart mysql || (log_format 'ERROR' "Failed to restart mysqld.service: Unit not found." && log_format "ERROR" "script_execute_error!" && exit 1)
    reok=false
    for ((i=0;i<${check_times};i++))
    do
        sleep 2
        mysqlnum=`ps -ef|grep -w 'bin/mysqld'|grep -v grep|wc -l`
        if [ $mysqlnum -eq 0 ];then
            sleep 1
            # log_format 'WARNING' "wait mysql to start "      
        else
            # log_format 'INFO' "mysql restart success"
            reok=true
            break
        fi
    done
    if [ $reok == false ];then
        log_format 'ERROR' "start mysql failed" && log_format "ERROR" "script_execute_error!" && exit 1
    fi
fi
#查看mysql主服务器状态
# log_format 'INFO' "begin to  check master status"
statusnum=`$mycmdpath -h$MIP -u$dbmuser -p$dbmpassword -e "show master status;"  2> /dev/null|grep bin |wc -l `
if [ $statusnum -eq 0 ];then
    log_format "ERROR" "script_execute_error!" && exit 1
else
    status=`$mycmdpath -h$MIP -u$dbmuser -p$dbmpassword -e "show master status;" 2> /dev/null |grep bin `
    # echo $status
    binlog=`echo $status|awk '{print $1}'| sed -e 's/^[ ]*//g' | sed -e 's/[ ]*$//g'`
    position=`echo $status|awk '{print $2}'| sed -e 's/^[ ]*//g' | sed -e 's/[ ]*$//g'`
    if [ ! "$binlog" ] || [ ! $position ];then
        log_format "ERROR" "script_execute_error!" && exit 1
    else
        expr 1 + ${position} &>/dev/null
        if [ $? -ne 0 ];then
            log_format "ERROR" "script_execute_error!" && exit 1
        fi
        # log_format "INFO" 'binlogname and postion value get success!'
    fi
fi

echo "$binlog&$position"
log_format "INFO" "script_execute_success!"